﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 *  MVC控制器基类。
 * 
 * 最后修改：2011-10-29
 * ************************************************************/
namespace Mozlite.Web
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using System.Web.Routing;
    using System.Collections.Generic;
    using Mozlite.Common;
    using Mozlite.Common.Configuration;
    using Mozlite.Web.UI;

    /// <summary>
    /// 控制器基类。
    /// </summary>
    /// <typeparam name="TApplication">应用程序类型。</typeparam>
    /// <typeparam name="TConfiguration">应用程序配置类型。</typeparam>
    public abstract class WebBaseController<TApplication, TConfiguration> : Controller
        where TApplication : ApplicationBase<TApplication, TConfiguration>, new()
        where TConfiguration :ConfigurationBase<TConfiguration>,new()
    {
        /// <summary>
        /// 获取当前应用程序上下文实例。
        /// </summary>
        public AppContext Current
        {
            get
            {
                return AppContext.Current;
            }
        }

        /// <summary>
        /// 获取当前页码。
        /// </summary>
        public int PageIndex {
            get {
                return RouteData.Get<int>("page", Current.PageIndex);
            }
        }

        /// <summary>
        /// 获取当前配置信息。
        /// </summary>
        public SiteSettings Settings {
            get {
                return SiteSettings.Current;
            }
        }

        /// <summary>
        /// 设置状态信息。
        /// </summary>
        /// <param name="message">信息字符串。</param>
        /// <param name="isError">是否为错误信息。</param>
        public void SetStatus(string message, bool isError = false) {
            ViewBag.__Status = message;
            ViewBag.__StatusType = isError;
        }

        /// <summary>
        /// 设置资源状态信息。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="isError">是否错误。</param>
        /// <param name="args">参数。</param>
        public void SetResourceStatus(object key, bool isError, params object[] args) {
            SetStatus(Local(key, args), isError);
        }

        /// <summary>
        /// 设置资源状态信息。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        public void SetResourceStatus(object key, params object[] args)
        {
            SetStatus(Local(key, args), false);
        }

        /// <summary>
        /// 获取页数。
        /// </summary>
        /// <param name="size">总记录数。</param>
        /// <param name="pageSize">每页显示数量。</param>
        /// <returns>返回页数。</returns>
        public int GetPages(int size, int pageSize = 20) {
            return (int)Math.Ceiling(size * 1.0 / pageSize);
        }

        private string areaName;
        /// <summary>
        /// 获取区域名称。
        /// </summary>
        public string AreaName
        {
            get
            {
                return areaName;
            }
        }

        private string controller;
        /// <summary>
        /// 获取控制器名称。
        /// </summary>
        public string Controller
        {
            get
            {
                return controller;
            }
        }

        private string action;
        /// <summary>
        /// 获取控制器名称。
        /// </summary>
        public string Action
        {
            get
            {
                return action;
            }
        }

        /// <summary>
        /// 重写执行方法。
        /// </summary>
        /// <param name="requestContext">当前请求上下文。</param>
        protected override void Execute(System.Web.Routing.RequestContext requestContext)
        {
            areaName =requestContext.RouteData.DataTokens["area"] as string;
            controller = requestContext.RouteData.GetRequiredString("controller");
            action = requestContext.RouteData.GetRequiredString("action");
            base.Execute(requestContext);
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string Local(string key, params object[] args)
        {
            return Resources.GetString(key, Controller, AreaName, args);
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string Local(object key, params object[] args)
        {
            return Resources.GetString(key, Controller, AreaName, args);
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string GLocal(string key, params object[] args)
        {
            return Resources.GetString(key, null, null, args);
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string GLocal(object key, params object[] args)
        {
            return Resources.GetString(key, null, null, args);
        }

        /// <summary>
        /// 获取应用程序当前实例。
        /// </summary>
        protected TApplication AppCurrent
        {
            get
            {
                return ApplicationBase<TApplication, TConfiguration>.Current;
            }
        }
        /// <summary>
        /// 获取当前应用程序配置实例对象。
        /// </summary>
        protected TConfiguration AppConfig
        {
            get
            {
                return AppCurrent.Configuration;
            }
        }

        /// <summary>
        /// 判断当前用户是否通过验证。
        /// </summary>
        protected bool IsAuthenticated
        {
            get
            {
                return User.Identity.IsAuthenticated;
            }
        }

        /// <summary>
        /// 获取当前用户。
        /// </summary>
        protected User Self
        {
            get
            {
                return Current.User;
            }
        }

        /// <summary>
        /// 返回当前用户档案实例。
        /// </summary>
        protected UserProfile SelfProfile {
            get {
                return Current.Profile;
            }
        }

        /// <summary>
        /// 获取当前用户ID。
        /// </summary>
        protected int SelfID
        {
            get
            {
                return Self.UserID;
            }
        }

        /// <summary>
        /// 获取当前用户名称。
        /// </summary>
        protected string SelfName
        {
            get
            {
                return Self.UserName;
            }
        }

        /// <summary>
        /// 获取当前用户昵称。
        /// </summary>
        protected string SelfNickName
        {
            get
            {
                return Self.NickName;
            }
        }

        /// <summary>
        /// 获取文件浏览路径。
        /// </summary>
        /// <param name="fileid">文件ID。</param>
        /// <returns>返回文件浏览路径。</returns>
        protected string GetFileBrowserUrl(int fileid)
        {
            return GetFileBrowserUrl((object)fileid);
        }

        /// <summary>
        /// 获取文件浏览路径。
        /// </summary>
        /// <param name="fileid">文件ID。</param>
        /// <returns>返回文件浏览路径。</returns>
        protected string GetFileBrowserUrl(object fileid = null)
        {
            return CommonUrls.GetFileBrowser(fileid.ToInt32(-1));
        }

        private string returnUrl;
        /// <summary>
        /// 返回URL地址。
        /// </summary>
        public string ReturnUrl {
            get {
                if (returnUrl == null)
                    returnUrl = this.Get<string>("ReturnUrl");
                return returnUrl;
            }
        }
    }
}
